package com.example.itcacp.service;

import com.example.itcacp.entity.Message;
import com.example.itcacp.entity.Message.MessageType;
import com.example.itcacp.entity.User;
import com.example.itcacp.repository.mybatis.MessageMapper;
import com.example.itcacp.config.SensitiveWordFilter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MessageService {

    @Resource
    private MessageMapper messageMapper;
    @Resource
    private SensitiveWordFilter sensitiveWordFilter;

    // 保存消息到数据库
    public void saveMessage(Message message) {
        message.setStatus("未读");
        message.setCreatedAt( new Timestamp ( System.currentTimeMillis () ).toLocalDateTime () );
        messageMapper.insert (message);
    }

    // 更新消息状态为已读
    public void markMessageAsRead(Long messageId) {
        Message message = messageMapper.selectById (messageId);
        if (message != null) {
            message.setStatus("已读");
            messageMapper.updateById (message);
        }
    }

    // 获取用户的各类未读消息数量
    public Map<String, Integer> getUnreadCounts(Long receiverId) {
        Map<String, Integer> counts = new HashMap<> ();

        // 系统消息未读数
        int systemCount = messageMapper.countByReceiverIdAndStatusAndMessageType(
                receiverId, "未读", MessageType.SYSTEM);

        // 内容相关消息未读数
        int contentCount = messageMapper.countByReceiverIdAndStatusAndMessageType(
                receiverId, "未读", MessageType.CONTENT);

        // 私信未读数
        int privateCount = messageMapper.countByReceiverIdAndStatusAndMessageType(
                receiverId, "未读", MessageType.PRIVATE);

        counts.put("system", systemCount);
        counts.put("content", contentCount);
        counts.put("private", privateCount);
        counts.put("total", systemCount + contentCount + privateCount);

        return counts;
    }

    public void markMessagesBySenderAsRead(Long senderId, Long receiverId) {

    }

    public PageInfo<Message> getChatMessages(Long senderId, Long receiverId, Integer pageNum, Integer pageSize, Message.MessageType messageType) {
        PageHelper.startPage ( pageNum, pageSize );
        List<Message> chatMessages = messageMapper.getChatMessages(senderId, receiverId, messageType);
        return PageInfo.of ( chatMessages );
    }

    public List<User> getContacts(Long userId) {
        return messageMapper.selectContactUsers ( userId );
    }

    public List<Message> getMessagesByUserId(Long userId) {
        return null;
    }

    public PageInfo<Message> getMessagesByUserIdAndType(Integer pageNum, Integer pageSize, Long userId, MessageType messageType) {
        PageHelper.startPage ( pageNum, pageSize );
        List<Message> messages = messageMapper.getMessagesByReceiverIdAndMessageType ( userId, messageType );
        return PageInfo.of ( messages );
    }

    public Message getMessageById(Long id) {
        return null;
    }

    public List<Message> getUserNotices(Long userId) {
        return null;
    }

    public void deleteMessage(Long messageId) {
        messageMapper.deleteById ( messageId );
    }

    public PageInfo<Message> getMessageByType(Integer pageNum, Integer pageSize, String content, MessageType messageType) {
        // 创建分页请求
        PageHelper.startPage ( pageNum, pageSize );
        List<Message> messageList = messageMapper.findByMessageTypeAndContent ( messageType, content );
        return PageInfo.of ( messageList );
    }
}

